﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TinyERP.Framework.Controls
{
    public class TreeNavigator : TreeView, INavigator
    {
        public event SelectNodeHandler OnSelectedNode;
        private TreeNode applicationNode;
        public TreeNavigator()
            : base()
        {
            this.AfterSelect += new TreeViewEventHandler(OnAfterSelect);
        }

        void OnAfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = this.SelectedNode;
            if (selectedNode == null || selectedNode.Tag == null)
            {
                return;
            }
            ApplicationNode selectedFormNode = selectedNode.Tag as ApplicationNode;
            if (OnSelectedNode != null)
            {
                OnSelectedNode(this, selectedFormNode);
            }
        }

        
        
        public void AddNode(ApplicationNode formNode)
        {
            if (applicationNode == null)
            {
                applicationNode = new TreeNode(ApplicationConfiguration.GetInstance().ApplicationName);
                this.Nodes.Add(applicationNode);
            }
            applicationNode.Nodes.Add(CreateTreeNode(formNode));
            this.ExpandAll();
        }

        public void SelectNode(ApplicationNode formNode)
        {
            TreeNode treeNode = FindTreeNodeContainForm(formNode);
            if (treeNode != null)
            {
                this.SelectedNode = treeNode;
            }
        }
        private TreeNode FindTreeNodeContainForm(ApplicationNode formNode)
        {
            foreach (TreeNode rootNode in this.Nodes)
            {
                TreeNode result = FindTreeNodeContainFormInBranch(rootNode, formNode);
                if (result != null)
                {
                    return result;
                }
            }
            return null;
        }

        private TreeNode FindTreeNodeContainFormInBranch(TreeNode treeNode, ApplicationNode formNode)
        {
            if (treeNode.Tag != null && ((ApplicationNode)treeNode.Tag) == formNode)
            {
                return treeNode;
            }
            if (treeNode.Nodes.Count != 0)
            {
                foreach (TreeNode child in treeNode.Nodes)
                {
                    TreeNode result = FindTreeNodeContainFormInBranch(child, formNode);
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            return null;
        }
        private TreeNode CreateTreeNode(ApplicationNode formNode)
        {
            TreeNode treeNode = new TreeNode(formNode.Title);
            treeNode.Tag = formNode;
            if (formNode.Children.Count > 0)
            {
                foreach (ApplicationNode childFormNode in formNode.Children)
                {
                    treeNode.Nodes.Add(CreateTreeNode(childFormNode));
                }
            }
            return treeNode;
        }
    }
}
